package com.ykt.controller;

import com.ykt.common.constant.ParamNames;
import com.ykt.common.utils.Colls;
import com.ykt.common.utils.UB;
import com.ykt.common.utils.page.PageData;
import com.ykt.common.utils.page.PageParam;
import com.ykt.converter.GoodsCategoryConverter;
import com.ykt.converter.GoodsConverter;
import com.ykt.model.Goods;
import com.ykt.model.GoodsCategory;
import com.ykt.service.GoodsCategoryService;
import com.ykt.service.GoodsService;
import com.ykt.vo.GoodsCategoryVo;
import com.ykt.vo.GoodsVo;
import com.ykt.vo.ResultVo;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 门店商品服务
 * @author: long.hua
 * @date: 2016-02-16 10:48
 * @since 1.0
 */
public class GoodsController extends BaseController{

    private GoodsService goodsService = enhance(GoodsService.class.getName(), GoodsService.class);
    private GoodsCategoryService categoryService = enhance(GoodsCategoryService.class.getName(), GoodsCategoryService.class);



    //商户提交商品价格
    public void changePrice(){
        int goodsId = UB.i(getPara(ParamNames.goodsId));
        double price = UB.d(getPara(ParamNames.price));

        goodsService.changePrice(goodsId, price);

        this.returnJson(ResultVo.success());
    }

    /**查询商铺的商品**/
    public void find(){
        // 组装参数
        int shopId = UB.i(getPara(ParamNames.shopId));

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put(ParamNames.shopId, shopId);
        PageParam<Map<String, Object>> param = super.toPageParam(paramMap);

        PageData<Goods> pd = goodsService.pageFindByShopId(param);
        if(pd == null) {
            returnJson(ResultVo.failure());
            return;
        }

        if(Colls.isEmpty(pd.getPageData())) {
            this.returnJson(ResultVo.success(pd));
            return;
        }

        this.returnJson(ResultVo.success(pd.convert(GoodsConverter.convert(pd.getPageData()))));
    }
    
    /**
     * 查询商铺的商品，商品上下架使用*
     */
    public void findCategoryGoods() {
        Integer shopId = getParaToInt("shopId", -1);
        String status = getPara("status");

        List<GoodsCategory> goodsCategories = categoryService.findByShopId(shopId);
        List<GoodsCategoryVo> categories = GoodsCategoryConverter.convert(goodsCategories);

        List<Goods> goodses = goodsService.findByShopId(shopId, status);
        List<GoodsVo> goodsVos = GoodsConverter.convert(goodses);

        Map<Integer, List<GoodsVo>> goodsMap = new LinkedHashMap<>();
        if (goodsVos != null && goodsVos.size() > 0) {
            for (GoodsVo goodsVo : goodsVos) {
                if (goodsMap.containsKey(goodsVo.getSgCatId())) {
                    goodsMap.get(goodsVo.getSgCatId()).add(goodsVo);
                } else {
                    goodsMap.put(goodsVo.getSgCatId(), Colls.array2List(goodsVo));
                }
            }
        }
        if (categories != null && categories.size() > 0) {
            for (GoodsCategoryVo vo : categories) {
                if (goodsMap.containsKey(vo.getCategoryId())) {
                    vo.setGoodses(goodsMap.get(vo.getCategoryId()));
                }
            }
        }

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("categories", categories);

        this.returnJson(ResultVo.success(dataMap));
    }

    /**商铺上下架*/
    public void modifyStatus(){
        int goodsId = UB.i(getPara(ParamNames.goodsId));
        String status = getPara(ParamNames.status);

        goodsService.changeStatus(goodsId, status);

        this.returnJson(ResultVo.success());
    }
}
